﻿using com.fmtx.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.fmtx.IDal;
using fmtx.Framework;
using fmtx.Framework.Expend;
using fmtx.Framework.Model;

namespace com.fmtx.Dal
{
    public class ArticleServer :BaseServer<Article>,IArticle
    {
        public new bool Add(Article bean)
        {
            UserInfoServer ui = new UserInfoServer();
            var u = ui.Get(bean.Uid);
            u.ArticleCount++;
            ui.Update(u);
            return base.Add(bean);
        }

        public new bool Delete(int id)
        {
            var a = db.Load(id);
            UserInfoServer ui = new UserInfoServer();
            var u = ui.Get(a.Uid);
            u.ArticleCount--;
            ui.Update(u);
            return base.Delete(id);
        }

        public IList<Article> Query(int typeId)
        {
            return db.AllList(w=>w.TypeId==typeId).ToList();
        }


        public IList<Article> Query(int index, int size)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass && !string.IsNullOrEmpty(w.FrontCover));
            list = list.OrderByDescending(o => o.Id).Skip(index * size).Take(size);
            return list.ToList();
        }


        public IList<Article> QureyHost(int index, int size, string title, string keyword)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass && !string.IsNullOrEmpty(w.FrontCover));
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w => w.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            
            list = list.OrderByDescending(o => o.ShowCount);
            return list.Skip(index*size).Take(size).ToList();
        }


        public IList<Article> Query(int size, int uid, string address)
        {
            var reslist = new List<Article>();
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass );
            if (list != null)
            {
                if (!string.IsNullOrEmpty(address))
                {
                    list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a=>a.Name==address));
                }
                foreach (var item in list.Where(w => w.Uid == uid).Take(size))
                {
                    reslist.Add(item);
                }
                foreach (var item in list.Take(size))
                {
                    reslist.Add(item);
                }
            }

            return reslist.DistinctBy(d=>d.Id).Take(size).ToList();
        }

        public IList<Article> QueryHost(int size)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass);
            return list.OrderByDescending(o => o.ShowCount).Take(size).ToList();
        }

        public IList<Article> QueryNew(int size)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass);
            return list.OrderByDescending(o => o.Id).Take(size).ToList();
        }


        public IList<Article> Query(int lastid, int size,int typeid, int uid, string title,bool ismy)
        {
            var list = db.AllList(w => w.Uid == uid);
            if (!ismy)
            {
                list=list.Where(w => w.IsView && w.Statu == Approved.IsPass);
            }
            if (typeid > 0)
            {
                list = list.Where(w=>w.TypeId==typeid);
            }
            if (lastid > 0)
            {
                list = list.Where(w => w.Id < lastid);
            }
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w=>w.Title.Contains(title));
            }
            return list.OrderByDescending(o => o.Id).Take(size).ToList();
        }


        public IList<Article> QureyPraise(int index, int size, string title, string keyword)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass && !string.IsNullOrEmpty(w.FrontCover));
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w => w.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }

            list = list.OrderByDescending(o => o.PraiseCount);
            return list.Skip(index * size).Take(size).ToList();
        }


        public bool HasSource(string source)
        {
            var model = db.AllList(w => w.Source == source).FirstOrDefault();
            if (model != null && model.Id > 0)
            {
                return true;
            }
            return false;
        }

        public IList<Article> Query()
        {
            return db.AllList().ToList();
        }


        public IList<Article> Query(int index, int size, string title,Approved statu,int uid, int cityid, int placeid,out int count)
        {
            var list= db.AllList();
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w => w.Title == title);
            }
            if (cityid > 0)
            {
                list = list.Where(w=>w.AreaKeys!=null&&w.AreaKeys.Any(a=>a.Id==cityid&&a.Type==AreaType.City));
            }
            if (placeid > 0)
            {
                list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a => a.Id == placeid && a.Type == AreaType.Place));
            }
            if (statu != Approved.AllStatu)
            {
                list = list.Where(w => w.Statu == statu);
            }
            if (uid > 0)
            {
                list = list.Where(w=>w.Uid==uid);
            }
            count = list.Count();
            return list.OrderByDescending(o => o.Id).Skip(index * size).Take(size).ToList();
        }


        public int GetCount()
        {
            return db.AllList().Count();
        }


        public IList<Article> QueryPraise(int size)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass);
            return list.OrderByDescending(o => o.PraiseCount).Take(size).ToList();
        }


        public IList<Article> Query(int index, int size, string keyword, int cityid, int placeid, int coutryid, int provincid,ref int count)
        {
            var list = db.AllList(w => w.IsView == true && w.Statu == Approved.IsPass);
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            if (cityid > 0)
            {
                list=list.Where(w =>w.AreaKeys!=null&& w.AreaKeys.Any(a => a.Id == cityid && a.Type == AreaType.City));
            }
            if (placeid > 0)
            {
                list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a => a.Id == placeid && a.Type == AreaType.Place));
            }
            if (coutryid > 0)
            {
                list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a => a.Id == coutryid && a.Type == AreaType.Country));
            }
            if (provincid > 0)
            {
                list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a => a.Id == provincid && a.Type == AreaType.Province));
            }
            count = list.Count();
            return list.OrderByDescending(o => o.Id).Skip(index * size).Take(size).ToList();
        }


        public IList<Article> QueryByKeyword(int size, string keyword)
        {
            var list = db.AllList(w => w.Statu == Approved.IsPass);
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            var relist=list.Take(size).ToList();
            int count = relist.Count;
            if (count < size)
            {
                List<int> idlist = relist.Select(s => s.Id).ToList();
                var otherlist = db.AllList(w => w.Statu == Approved.IsPass && !idlist.Contains(w.Id)).Take(size - count).ToList();
                relist.AddRange(otherlist);
            }
            return relist;
        }
    }
}
